package cn.summit;

import cn.summit.realm.MyRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @author summit
 * @since 2020/12/18 22:10
 */
public class CustomRealmMainTest {

    public static void main(String[] args) {
        // 1.安全管理对象
        DefaultSecurityManager securityManager = new DefaultSecurityManager();

        //2.realm
        securityManager.setRealm(new MyRealm());

        //3.给安全工具设置 安全管理器
        SecurityUtils.setSecurityManager(securityManager);

        //4.获取主体对象
        Subject subject = SecurityUtils.getSubject();

        // 身份凭证
        UsernamePasswordToken token = new UsernamePasswordToken("summit2", "a18d2133f593d7b0e3ed488560404083");
        System.out.println(subject.isAuthenticated());
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            System.out.println("账号不存在");
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码错误");
        }
        System.out.println(subject.isAuthenticated());

    }
}
